package com.ls.lserp.modules.order.service;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.order.dao.AuditInfoDao;
import com.ls.lserp.modules.order.dao.QuotationDao;
import com.ls.lserp.modules.order.entity.AuditInfo;
import com.ls.lserp.modules.order.entity.BigcargoOrder;
import com.ls.lserp.modules.order.entity.Quotation;
import com.ls.lserp.modules.setting.dao.WarehouseShelvesDao;
import com.ls.lserp.modules.setting.entity.CompanyList;
import com.ls.lserp.modules.setting.entity.WarehouseShelves;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.DictUtils;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
import com.ls.lserp.modules.sys.utils.UserUtils;
@Service
@Transactional(readOnly = false)
public class QuotationService extends CrudService<QuotationDao,Quotation>{

	
	 @Autowired
	    private QuotationDao quotationDao ;
	// 审核表
		@Autowired
		    private AuditInfoDao  auditInfoDao;

	    /**
		 * 查询父项
		 * @param page
		 * @param user
		 * @return
		 */
	    public Page<Quotation> findQuotationList(Page<Quotation> page, Quotation quotation) {
	        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
	        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
	        // 设置分页参数
	    	quotation.setPage(page);
	        // 执行分页查询
	        page.setList(quotationDao.findList(quotation));
	        return page;
	    }
	    
	    /**
		 * 查询款式
		 * @param page
		 * @param user
		 * @return
		 */
	    public Page<Quotation> findStyleList(Page<Quotation> page, Quotation quotation) {
	        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
	        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
	        // 设置分页参数
	    	quotation.setPage(page);
	        // 执行分页查询
            if("5".equals(quotation.getFrameFlg())){
                //外发加工合同选择的样式是大货的样式
                page.setList(quotationDao.findStyleDaHuoList(quotation));
            }
           else{
                page.setList(quotationDao.findStyleList(quotation));
            }
	        return page;
	    }
	    
	    /**
		 * 查询一条数据
		 * 
		 * @param user
		 * @return
		 */
	    public Quotation findQuotation(Quotation quotation){
	    	List<Quotation> material = quotationDao.findOfferMaterialList(quotation.getId());
	    	List<Quotation> historyList = quotationDao.findHistoryList(quotation);
	    	quotation = quotationDao.get(quotation);
	    	List<Quotation> styleFileList  = quotationDao.findStyleFileList(quotation.getStyleID());
	    	quotation.setStyleFileList(styleFileList);
	    	quotation.setQuotationHistoryList(historyList);
	    	quotation.setQuotationMaterialList(material);
	    	return quotation;
	    	
	    }
	    
	    /**
		 * 新增/修改一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void saveQuotation(Quotation quotation) {
	    	// 判断新建编辑
	    			//若为null 执行插入语句（新建）
	    			if (quotation.getId()==null){
	    		        Sequence sequence=SequenceUtil.offerCode("30", "sequence_quotation", 2 , quotation.getStyleNO());
	    				quotation.setOfferNO(sequence.getReturnCode());
	    				quotation.setOfferNumber(0);
	    				quotation.setCreateBy(UserUtils.getUser());	    				
	    				quotation.setPhase(1);
	    				quotation.setStatus(2);
	    				quotationDao.insert(quotation);
	    				quotationDao.insertOfferStyle(quotation);
	    				quotationDao.insertHistory(quotation);
	    				if (quotation.getQuotationMaterialList()!=null) {
	    				quotationDao.insertMaterial(quotation);
	    				quotationDao.insertMaterialHistory(quotation);
	    				}
	    				
	    			// 编辑
	    			}else{
	    				// 更新数据
	    				long offerNumber = quotation.getOfferNumber();
	    				quotation.setOfferNumber(offerNumber+1);
	    				quotationDao.update(quotation);
	    				quotationDao.deleteOfferStyle(quotation);
	    				quotationDao.insertOfferStyle(quotation);
	    				quotationDao.insertHistory(quotation);
	    				quotationDao.delete(quotation);
	    				if (quotation.getQuotationMaterialList()!=null) {
	    				quotationDao.insertMaterial(quotation);
	    				quotationDao.insertMaterialHistory(quotation);
	    				}
	    			
		    				
	    		}			
	    		
	    }
	    /**
		 * 判断字符串
		 * 
		 * @param user
		 * @return
		 */
	    public String checkStr(String str) {
			if ("undefined".equals(str)||str==null) {
				str = "";
			}
	    	return str;
		}
	    /**
		 * ID查询一条数据
		 * 
		 * @param user
		 * @return
		 */
	    public Quotation findByID(Long id){
	    	
	    	return quotationDao.get(id);
	    }    
	    /**
		 * ID查询一条款式信息
		 * 
		 * @param user
		 * @return
		 */
	    public Quotation getStyle(Quotation quotation){
	    	quotation = quotationDao.getStyle(quotation);
	    	List<Quotation> styleFileList  = quotationDao.findStyleFileList(quotation.getStyleID());
	    	quotation.setStyleFileList(styleFileList);
	    	return  quotation;
	    }   
	    
	    /**
		 * 款式ID查询单耗信息
		 * 
		 * @param user
		 * @return
		 */
	    public List<Quotation> findUnitCostList(Long id){
	    	
	    	return quotationDao.findUnitCostList(id);
	    } 
	    
	    /**
		 * ID查询一条单耗信息
		 * 
		 * @param user
		 * @return
		 */
	    public Quotation getUnitCost(Long id){
	    	
	    	List<Quotation> materialList = quotationDao.findMaterialList(id);
	    	Quotation quotation = new Quotation();
	    	quotation = quotationDao.getUnitCost(id);
	    	quotation.setQuotationMaterialList(materialList);
	    	
	    	return quotation;
	    } 
	    
	    
   
	    /**
	     * @Title: findAuditList
	     * @Description: 查询所有履历
	     * @param @param auditInfo
	     * @param @return
	     * @return List<AuditInfo> 
	     * @throws
	     */
	   public AuditInfo findAuditInfo(AuditInfo auditInfo) {
		   AuditInfo audit = new AuditInfo();
		   audit.setAuditList(auditInfoDao.findAllList(auditInfo));
		   audit.setAuditStatus(auditInfoDao.findAuditAcceptCount(auditInfo));
	       // 执行查询
	       return audit;
	   }
	   /**
	    * @Title: getAuditResult
	    * @Description: 审核结果
	    * @param @param auditInfo
	    * @param @return
	    * @return 审核结果通过数
	    * @throws
	    */
	  
	  public long getAuditResult(AuditInfo auditInfo) {
		   
	      return auditInfoDao.findAuditAcceptCount(auditInfo);
	  }
	  
	  /**
		  * @Title: saveAudit
		  * @Description: 保存履历信息
		  * @param @param auditInfo
		  * @return void 
		  * @throws
		  */
		@Transactional(readOnly = false)
		public void saveAuditInfo(AuditInfo auditInfo,boolean isUpdateStatus) {
			
			 // 插入履历表
		     auditInfo.preInsert();
		     auditInfo.setAuditDate(new Date());
		     auditInfo.setAuditUserId(UserUtils.getUser().getId());
	         // 新建版本号赋值1
		     auditInfo.setVersion(1L);
		     auditInfoDao.insert(auditInfo);
		     
		     auditInfo.preUpdate();
		     quotationDao.updateStatus(auditInfo);
		     
		     
		}
}
